//
//  YTStashPutAndOutShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/19.
//

#import "YTStashPutAndOutShowView.h"
#import "DigitalInputView.h"

#import "YTBaseSelectStringView.h"
#import "TemplateFoundDetailsModel.h"
#import "PersonInCchargeShowView.h"

@interface YTStashPutAndOutShowView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) YTStashListModel * model;
@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;

@property (nonatomic ,strong) UIButton * ckBtn;
@property (nonatomic ,strong) UIButton * rkBtn;
@property (nonatomic ,strong) UIButton * SeledBtn;
@property (nonatomic ,strong) UIView * lineview;
@property (nonatomic ,strong) UITableView * tableview;
@property (nonatomic ,strong) void(^block)(void);

@end
@implementation YTStashPutAndOutShowView

+ (instancetype)showviewWithModel:(YTStashListModel *)model andBlock:(void(^)(void))block{
    YTStashPutAndOutShowView * view = [YTStashPutAndOutShowView new];
    model.expiratDate = @"";
    view.model = model;
    view.block = block;
    [view setupview];
    [view show];
    return view;
}

- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(515);
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  self.viewH);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH,  self.viewH)];
    
    
    UIView * topview = [UIView new];
    [makeView addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(55));
    }];
    
    UIButton * ckBtn = [UIButton buttonWithType:0];
    self.ckBtn = ckBtn;
    ckBtn.addNormalText(@"出库").addNormalColor(KcolorHex(@"#222222")).addFont(kCFont(19)).addTarget(self,@selector(ckBtnClick));
    [ckBtn setTitleColor:KcolorHex(@"#207EFA") forState:UIControlStateSelected];
    [topview addSubview:ckBtn];
    [ckBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
    }];
    UIButton * rkBtn = [UIButton buttonWithType:0];
    self.rkBtn = rkBtn;
    rkBtn.addNormalText(@"入库").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(19)).addTarget(self,@selector(rkBtnClick));
    [rkBtn setTitleColor:KcolorHex(@"#207EFA") forState:UIControlStateSelected];
    [topview addSubview:rkBtn];
    [rkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.offset(0);
        make.left.equalTo(ckBtn.mas_right);
        make.width.equalTo(ckBtn);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#E2E2E2");
    [topview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.offset(0);
        make.height.offset(kwt(2));
    }];
    
    UIView * lineview = [UIView new];
    self.lineview = lineview;
    lineview.backgroundColor = KcolorHex(@"#207EFA");
    [line addSubview:lineview];
    lineview.frame = CGRectMake(0, 0, KSCREEN_WIDTH/2, kwt(2));
    [self ckBtnClick];
    
    
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    UIView * line1 = [UIView new];
    line1.backgroundColor = KcolorHex(@"#ECECEC");
    [buttonview addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(0.5);
    }];
    
    UIButton * leftbtn = [UIButton buttonWithType:0];
    leftbtn.addNormalText(@"取消").addNormalColor(KcolorHex(@"#484848")).addFont(kCFont(18)).addbackgroundColor(KcolorHex(@"#EDEDED")).addTarget(self,@selector(dismiss));
    [DTKTool YJ:kwt(10) andView:leftbtn];
    [buttonview addSubview:leftbtn];
    [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(8));
        make.width.offset(kwt(133));
        make.height.offset(kwt(48));
    }];
    
    UIButton * rightbtn = [UIButton buttonWithType:0];
    rightbtn.addNormalText(@"确定").addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(196), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]).addTarget(self,@selector(QDBtnClick));
    [DTKTool YJ:kwt(10) andView:rightbtn];
    [buttonview addSubview:rightbtn];
    [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.top.offset(kwt(8));
        make.left.equalTo(leftbtn.mas_right).offset(kwt(17));
        make.height.offset(kwt(48));
    }];
    
    [makeView addSubview:self.tableview];
    //    self.tableview.frame = CGRectMake(0, kwt(55), KSCREEN_WIDTH, self.viewH - kwt(55) - (kwt(63) + KSafeAreaBottomHeight));
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    [self ckBtnClick];
    
}
///确定
- (void)QDBtnClick{
    if(self.SeledBtn == self.ckBtn){///出库
        if(self.model.stockNumber < 1){
            [self showWarning:@"库存不足"];
            return;
        }
        [self showBusyHUD];
        [SCNetManger warehouseManageOutWithDict: [self.model CKDict] andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self hideBusyHUD];
            if(model.code ==200){
                [self showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.block();
                    // 2秒后执行的代码
                    [self dismiss];
                });
            } else {
                [self showWarning:model.message];
            }
        }];
        
        
    } else if(self.SeledBtn == self.rkBtn){///入库
        if(self.model.expiratDate.length < 1){
            [self showWarning:@"请选择保质日期"];
            return;
        }
        if(self.model.deliveryId.length < 1){
            [self showWarning:@"请选择交货人"];
            return;
        }
        if(self.model.storemanId.length < 1){
            [self showWarning:@"请选择仓管员"];
            return;
        }
        
        [self showBusyHUD];
        [SCNetManger warehouseManageIntoWithDict:[self.model RKDict] andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self hideBusyHUD];
            if(model.code ==200){
                [self showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.block();
                    // 2秒后执行的代码
                    [self dismiss];
                });
            } else {
                [self showWarning:model.message];
            }
        }];
    }
}
- (void)ckBtnClick{
    if(self.SeledBtn != self.ckBtn){
        self.rkBtn.addFont(kFont(19));
        self.rkBtn.selected = NO;
        self.SeledBtn = self.ckBtn;
        self.SeledBtn.addFont(kCFont(19));
        self.SeledBtn.selected = YES;
        if(self.model.stockNumber < 1){
            self.model.number = @"0";
        } else {
            self.model.number = @"1";
        }
        self.model.isRK = NO;
        MJWeakSelf
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.lineview.frame =  CGRectMake(0, 0, KSCREEN_WIDTH/2, kwt(2));
        }];
        [self.tableview reloadData];
    }
}

- (void)rkBtnClick{
    if(self.SeledBtn != self.rkBtn){
        self.SeledBtn.addFont(kFont(19));
        self.SeledBtn.selected = NO;
        self.SeledBtn = self.rkBtn;
        self.SeledBtn.addFont(kCFont(19));
        self.SeledBtn.selected = YES;
        self.model.number = @"1";
        self.model.isRK = YES;
        MJWeakSelf
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.lineview.frame =  CGRectMake(KSCREEN_WIDTH/2, 0, KSCREEN_WIDTH/2, kwt(2));
        }];
        [self.tableview reloadData];
    }
}


- (UITableView *)tableview{
    if(!_tableview){
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kwt(120), SCREEN_WIDTH, SCREEN_HEIGHT - kwt(120)) style:UITableViewStyleGrouped];
        
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        if (@available(iOS 11.0, *)) {
            _tableview.estimatedRowHeight = 0;
            _tableview.estimatedSectionHeaderHeight = 0;
            _tableview.estimatedSectionFooterHeight = 0;
            _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableview.sectionHeaderTopPadding = 0;
            _tableview.fillerRowHeight = 0;
        }
        _tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionHeaderHeight = 0.01;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionFooterHeight = 0.01;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColor.clearColor;
    }
    return _tableview;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YTStashPutAndOutShowHeaderView * view = [YTStashPutAndOutShowHeaderView initview];
    view.model = self.model;
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(self.SeledBtn == self.ckBtn){///出库带数量
        return kwt(150);
    }
    return kwt(115);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(self.SeledBtn == self.ckBtn){///出库带数量
        return kwt(62);
    }
    return kwt(62 * 4);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YTStashPutAndOutShowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStashPutAndOutShowTableViewCell"];
    if (cell == nil) {
        cell = [[YTStashPutAndOutShowTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStashPutAndOutShowTableViewCell"];
    }
    cell.model = self.model;
    MJWeakSelf
    cell.openHUDBlock = ^(NSString * _Nonnull str) {
        [weakSelf showWarning:str];
        [weakSelf.tableview reloadData];
    };
    cell.openReloadBlock = ^{
        [weakSelf.tableview reloadData];
    };
    cell.openBZRQBlock = ^(YTStashListModel * _Nonnull model) {
        [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"请选择保质日期" selectValue:model.expiratDate minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            weakSelf.model.expiratDate = selectValue;
            [weakSelf.tableview reloadData];
        }];
    };
    cell.openJHRBlock = ^(YTStashListModel * _Nonnull model) {
        [weakSelf openJHR:model];
    };
    cell.openCGYBlock = ^(YTStashListModel * _Nonnull model) {
        [weakSelf openCGY:model];
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
- (void)show{
    [[DTKTool getCurrentVC].view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

- (void)openJHR:(YTStashListModel *)Model{
    [self showBusyHUD];
    [SCNetManger PostGetPositionAndUserByOrgCodeWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self hideBusyHUD];
        if(model.code ==200){
            NSArray <PositionUserModel *>* modelarray = [NSArray yy_modelArrayWithClass:PositionUserModel.class json:model.result];
            
            for (PositionUserModel * pmodel in modelarray) {
                for (PondUserModel * usermodel in pmodel.userModelList) {
                    if ([Model.deliveryId isEqualToString:usermodel.userId]) {
                        usermodel.isSeled = YES;
                    }
                }
            }
            MJWeakSelf
            [SearchPersonInCchargeShowView showviewWithTitle:@"交货人" andArray:modelarray andBlock:^(NSString * _Nonnull ID) {
                for (PositionUserModel * pmodel in modelarray) {
                    for (PondUserModel * usermodel in pmodel.userModelList) {
                        if ([ID isEqualToString:usermodel.userId]) {
                            weakSelf.model.deliveryId  = usermodel.userId;
                            weakSelf.model.deliveryName = usermodel.realName;
                            [weakSelf.tableview reloadData];
                            return;
                        }
                    }
                }
            }];
        } else {
            [self showWarning:model.message];
        }
    }];
}
///仓管员
- (void)openCGY:(YTStashListModel *)Model{
    [self showBusyHUD];
    [SCNetManger PostGetPositionAndUserByOrgCodeWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self hideBusyHUD];
        if(model.code ==200){
            NSArray <PositionUserModel *>* modelarray = [NSArray yy_modelArrayWithClass:PositionUserModel.class json:model.result];
            
            for (PositionUserModel * pmodel in modelarray) {
                for (PondUserModel * usermodel in pmodel.userModelList) {
                    if ([self.model.storemanId isEqualToString:usermodel.userId]) {
                        usermodel.isSeled = YES;
                    }
                }
            }
            MJWeakSelf
            [SearchPersonInCchargeShowView showviewWithTitle:@"仓管员" andArray:modelarray andBlock:^(NSString * _Nonnull ID) {
                
                for (PositionUserModel * pmodel in modelarray) {
                    for (PondUserModel * usermodel in pmodel.userModelList) {
                        if ([ID isEqualToString:usermodel.userId]) {
                            weakSelf.model.storemanId  = usermodel.userId;
                            weakSelf.model.storemanName = usermodel.realName;
                            [weakSelf.tableview reloadData];
                            return;
                        }
                    }
                }
            }];
        } else {
            [self showWarning:model.message];
        }
    }];
}
@end

@interface YTStashPutAndOutShowHeaderView()

@property (nonatomic ,strong) UIImageView * productImg;
@property (nonatomic ,strong) UILabel * name;
@property (nonatomic ,strong) UILabel * productBrand;
@property (nonatomic ,strong) UILabel * stockNumber;

@end
@implementation YTStashPutAndOutShowHeaderView
+ (instancetype)initview{
    YTStashPutAndOutShowHeaderView * view = [YTStashPutAndOutShowHeaderView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    [view setupview];
    return view;
}
- (void)setupview{
    UIImageView * productImg = kimageView(@"tangkou_photo");
    self.productImg = productImg;
    [DTKTool YJ:kwt(4) andView:productImg];
    [self addSubview:productImg];
    [productImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.width.height.offset(kwt(66));
        make.top.offset(kwt(23));
    }];
    
    UILabel * name = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.name = name;
    name.numberOfLines = 2;
    [self addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(23));
        make.left.offset(kwt(92));
        make.right.offset(kwt(-12));
    }];
    
    UILabel * pplab = [DTKTool labelWithText:@"品牌" andFont:kFont(13) andTextColor:KcolorHex(@"#FEF8E3")];
    pplab.textAlignment = NSTextAlignmentCenter;
    [DTKTool BFYJ:kwt(3) andView:pplab andCorner:UIRectCornerTopLeft|UIRectCornerBottomLeft andSize:CGRectMake(0,0,kwt(32), kwt(21))];
    pplab.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(32), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#4F5680") endColor:KcolorHex(@"#252638")];
    [self addSubview:pplab];
    [pplab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(77));
        make.left.offset(kwt(92));
        make.width.offset(kwt(32));
        make.height.offset(kwt(21));
    }];
    UILabel * productBrand = [DTKTool labelWithText:@"--" andFont:kFont(13) andTextColor:KcolorHex(@"#6E3E00")];
    self.productBrand = productBrand;
    productBrand.textAlignment = NSTextAlignmentCenter;
    [DTKTool BFYJ:kwt(3) andView:productBrand andCorner:UIRectCornerTopRight|UIRectCornerBottomRight andSize:CGRectMake(0,0,kwt(32), kwt(21))];
    productBrand.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(32), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#FFE5CC") endColor:KcolorHex(@"#FFEBC2")];
    [self addSubview:productBrand];
    [productBrand mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pplab.mas_right);
        make.centerY.equalTo(pplab);
        make.width.offset(kwt(32));
        make.height.offset(kwt(21));
    }];
    
    UILabel * temp = [DTKTool labelWithText:@"库存数量:" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [self addSubview:temp];
    [temp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(113));
        make.left.offset(kwt(92));
    }];
    
    UILabel * stockNumber = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.stockNumber = stockNumber;
    [self addSubview:stockNumber];
    [stockNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(temp);
        make.left.equalTo(temp.mas_right).offset(kwt(4));
    }];
    
}

- (void)setModel:(YTStashListModel *)model{
    _model = model;
    [self.productImg LZsetImageWithImagePath:model.productImg placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
    self.name.text = model.name;
    self.productBrand.text = model.productBrand;
    CGFloat productBrandW = [DTKTool textWithtitle:model.productBrand andSize:CGSizeMake(1000, 100) andFout:kFont(13)].width + kwt(8);
    if(productBrandW > kwt(220)){
        productBrandW = kwt(220);
    }
    [DTKTool BFYJ:kwt(3) andView:self.productBrand andCorner:UIRectCornerTopRight|UIRectCornerBottomRight andSize:CGRectMake(0,0,productBrandW, kwt(21))];

    [self.productBrand mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(productBrandW);
    }];
    self.stockNumber.text = [NSString stringWithFormat:@"%ld%@",(long)model.stockNumber,model.normsName];
}
@end

@interface YTStashPutAndOutShowTableViewCell ()
@property (nonatomic ,strong) UILabel * name;
@property (nonatomic ,strong) DigitalInputView * digitalInputView;
@property (nonatomic ,strong) YTBaseSelectStringView * timeview;
@property (nonatomic ,strong) YTBaseSelectStringView * jhrview;
@property (nonatomic ,strong) YTBaseSelectStringView * cgyview;



@end
@implementation YTStashPutAndOutShowTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    self.contentView.clipsToBounds = YES;
    
    
    UIView * makeview = [UIView new];
    makeview.backgroundColor = KcolorHex(@"#F6F6F6");
    [DTKTool YJ:kwt(5) andView:makeview];
    [self.contentView addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(5));
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.height.offset(kwt(52));
    }];
    
    UILabel * name = [DTKTool labelWithText:@"入库数量" andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
    [makeview addSubview:name];
    self.name = name;
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.centerY.offset(0);
    }];
    
    DigitalInputView * inputview = [DigitalInputView DKNewView];
    self.digitalInputView = inputview;
    [makeview addSubview:inputview];
    MJWeakSelf
    inputview.openReloadBlock = ^{
        weakSelf.model.number = [NSString stringWithFormat:@"%@",weakSelf.digitalInputView.model.number];
        if(weakSelf.openReloadBlock){
            weakSelf.openReloadBlock();
        }
    };
    inputview.openHUDBlock = ^(NSString * _Nonnull str) {
        weakSelf.model.number = [NSString stringWithFormat:@"%@",weakSelf.digitalInputView.model.number];
        if(weakSelf.openHUDBlock){
            weakSelf.openHUDBlock(str);
        }
    };
    [inputview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.centerY.offset(0);
        make.width.offset(kwt(170));
        make.height.offset(kwt(32));
    }];
    
    YTBaseSelectStringView * timeview = [YTBaseSelectStringView NewViewWithNil];
    self.timeview = timeview;
    timeview.name = @"保质日期";
    timeview.contentMRStr = @"选择保质日期";
    timeview.forceFlag = NO;
    timeview.font = kFont(17);
    [self.contentView addSubview:timeview];
    [timeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(makeview.mas_bottom).offset(kwt(10));
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.height.offset(kwt(52));
    }];
    timeview.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openBZRQBlock){
            weakSelf.openBZRQBlock(weakSelf.model);
        }
    };
    
    YTBaseSelectStringView * jhrview = [YTBaseSelectStringView NewViewWithNil];
    jhrview.name = @"交货人";
    jhrview.forceFlag = NO;
    self.jhrview = jhrview;
    jhrview.font = kFont(17);
    [self.contentView addSubview:jhrview];
    jhrview.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openJHRBlock){
            weakSelf.openJHRBlock(weakSelf.model);
        }
    };
    
    [jhrview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeview.mas_bottom).offset(kwt(10));
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.height.offset(kwt(52));
    }];
    
    YTBaseSelectStringView * cgyview = [YTBaseSelectStringView NewViewWithNil];
    cgyview.name = @"仓管员";
    cgyview.forceFlag = NO;
    self.cgyview =cgyview;
    cgyview.font = kFont(17);
    cgyview.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openCGYBlock){
            weakSelf.openCGYBlock(weakSelf.model);
        }
    };
    
    [self.contentView addSubview:cgyview];
    [cgyview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jhrview.mas_bottom).offset(kwt(10));
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.height.offset(kwt(52));
    }];
    
    
}

- (void)setModel:(YTStashListModel *)model{
    _model = model;
    TemplateFoundDetailsTaskItemModelListModel * m = [TemplateFoundDetailsTaskItemModelListModel new];
    if(model.isRK){///入库不限制
        m.numberMax = 99999999;
        self.name.text = @"入库数量";
    } else {
        m.numberMax = model.stockNumber;
        self.name.text = @"出库数量";
    }
    if(model.stockNumber < 1){
        m.numberMin = 0;
    } else {
        m.numberMin = 1;
    }
    m.degree = 0;
    m.number = [NSNumber numberWithInt:model.number.intValue];
    m.stepValue = 1;
    self.digitalInputView.model = m;
    self.timeview.content = model.expiratDate;
    self.jhrview.content = model.deliveryName;
    self.cgyview.content = model.storemanName;
}

@end



@interface YTStashOutShowView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) YTStashListModel * model;
@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;

@property (nonatomic ,strong) UITableView * tableview;
@property (nonatomic ,strong) void(^block)(void);

@end
@implementation YTStashOutShowView

+ (instancetype)showviewWithModel:(YTStashListModel *)model andBlock:(void(^)(void))block{
    YTStashOutShowView * view = [YTStashOutShowView new];
    if(model.stockNumber < 1){
        model.number = @"0";
    } else {
        model.number = @"1";
    }
    view.model = model;
    view.block = block;
    [view setupview];
    [view show];
    return view;
}

- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(515);
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  self.viewH);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH,  self.viewH)];
    
    
    UIView * topview = [UIView new];
    [makeView addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(55));
    }];
    
    UIButton * ckBtn = [UIButton buttonWithType:0];
    ckBtn.addNormalText(@"出库").addNormalColor(KcolorHex(@"#207EFA")).addFont(kCFont(19));
    [ckBtn setTitleColor:KcolorHex(@"#207EFA") forState:UIControlStateSelected];
    [topview addSubview:ckBtn];
    [ckBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
        make.width.offset(kwt(78));
    }];
 
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#E2E2E2");
    [topview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.offset(0);
        make.height.offset(kwt(2));
    }];
    
   
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    UIView * line1 = [UIView new];
    line1.backgroundColor = KcolorHex(@"#ECECEC");
    [buttonview addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(0.5);
    }];
    
    UIButton * leftbtn = [UIButton buttonWithType:0];
    leftbtn.addNormalText(@"取消").addNormalColor(KcolorHex(@"#484848")).addFont(kCFont(18)).addbackgroundColor(KcolorHex(@"#EDEDED")).addTarget(self,@selector(dismiss));
    [DTKTool YJ:kwt(10) andView:leftbtn];
    [buttonview addSubview:leftbtn];
    [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(8));
        make.width.offset(kwt(133));
        make.height.offset(kwt(48));
    }];
    
    UIButton * rightbtn = [UIButton buttonWithType:0];
    rightbtn.addNormalText(@"确定").addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(196), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]).addTarget(self,@selector(QDBtnClick));
    [DTKTool YJ:kwt(10) andView:rightbtn];
    [buttonview addSubview:rightbtn];
    [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.top.offset(kwt(8));
        make.left.equalTo(leftbtn.mas_right).offset(kwt(17));
        make.height.offset(kwt(48));
    }];
    
    [makeView addSubview:self.tableview];
    //    self.tableview.frame = CGRectMake(0, kwt(55), KSCREEN_WIDTH, self.viewH - kwt(55) - (kwt(63) + KSafeAreaBottomHeight));
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    
}
///确定
- (void)QDBtnClick{
    if(self.model.stockNumber < 1){
        [self showWarning:@"库存不足"];
        return;
    }
        [self showBusyHUD];
        [SCNetManger warehouseManageOutWithDict: [self.model CKDict] andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self hideBusyHUD];
            if(model.code ==200){
                [self showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.block();
                    // 2秒后执行的代码
                    [self dismiss];
                });
            } else {
                [self showWarning:model.message];
            }
        }];
        
   
}

- (UITableView *)tableview{
    if(!_tableview){
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kwt(120), SCREEN_WIDTH, SCREEN_HEIGHT - kwt(120)) style:UITableViewStyleGrouped];
        
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        if (@available(iOS 11.0, *)) {
            _tableview.estimatedRowHeight = 0;
            _tableview.estimatedSectionHeaderHeight = 0;
            _tableview.estimatedSectionFooterHeight = 0;
            _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableview.sectionHeaderTopPadding = 0;
            _tableview.fillerRowHeight = 0;
        }
        _tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionHeaderHeight = 0.01;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionFooterHeight = 0.01;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColor.clearColor;
    }
    return _tableview;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YTStashPutAndOutShowHeaderView * view = [YTStashPutAndOutShowHeaderView initview];
    view.model = self.model;
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return kwt(150);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kwt(62);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YTStashPutAndOutShowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStashPutAndOutShowTableViewCell"];
    if (cell == nil) {
        cell = [[YTStashPutAndOutShowTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStashPutAndOutShowTableViewCell"];
    }
    cell.model = self.model;
    MJWeakSelf
    cell.openHUDBlock = ^(NSString * _Nonnull str) {
        [weakSelf showWarning:str];
        [weakSelf.tableview reloadData];
    };
    cell.openReloadBlock = ^{
        [weakSelf.tableview reloadData];
    };
    cell.openBZRQBlock = ^(YTStashListModel * _Nonnull model) {
        [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"请选择保质时间" selectValue:model.expiratDate minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            weakSelf.model.expiratDate = selectValue;
            [weakSelf.tableview reloadData];
        }];
    };
  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
- (void)show{
    [[DTKTool getCurrentVC].view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

@end
